package com.gobuy.page.mqlistener;

import com.gobuy.canal.config.RabbitMQConfig;
import com.gobuy.page.service.PageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author Gaozexi on 2020/6/11 16:40
 * Step by Step  and Stand on,  You Are The Best Investment!
 */

/**
 * 商品审核消息监听
 */
@Component
public class PageListener {

    @Autowired
    private PageService pageService;

    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @RabbitListener(queues = RabbitMQConfig.PAGE_CREATE_QUEUE)
    public void createPagesOnMessage(String spuId){
        //id:ADD   id:DELETE
        String action = spuId.split(":")[1];
        spuId = spuId.split(":")[0];
        if("ADD".equalsIgnoreCase(action)){
            logger.info("spuId="+spuId+"的商品 已通过审核,准备生成相应商品详情页");
            try {
                pageService.generateItemPage(spuId);
            } catch (Exception e) {
                logger.error("["+spuId+"]商品详情页生成失败");
                e.printStackTrace();
            }
        }
        if("DELETE".equalsIgnoreCase(action)){
            logger.info("spuId="+spuId+"的商品 撤销审核,准备删除相应商品详情页");
            try {
                pageService.generateItemPage(spuId);
            } catch (Exception e) {
                logger.error("["+spuId+"]商品详情页生成失败");
                e.printStackTrace();
            }
        }

    }

}
